package com.example.threadtest.controller;

import com.example.threadtest.service.impl.AsyncServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

/**
 * @author 越
 * @createDate 2021-04-06 16:12
 * @desc
 */
@RestController
public class AsyncController {

    private final static Logger logger = LoggerFactory.getLogger(AsyncController.class);

    @Autowired
    private AsyncServiceImpl asyncService;

    @SuppressWarnings("static-access")
    @RequestMapping(value = { "/test" }, method = RequestMethod.GET)
    public void index() {
        long start = System.currentTimeMillis();
        try {
            logger.info("--------------------------------------------\n");
            logger.info("每个任务执行的时间是：" + asyncService.DoTime + "（毫秒）");

            Future<Long> task = asyncService.subByAsync();

            asyncService.subByVoid();

            long sync = asyncService.subBySync();

            while (true) {
                if (task.isDone()) {
                    long async = task.get();
                    logger.info("异步任务执行的时间是：" + async + "（毫秒）");
                    // logger.info("注解任务执行的时间是： -- （毫秒）");
                    logger.info("同步任务执行的时间是：" + sync + "（毫秒）");
                    break;
                }
            }
            logger.info("--------------------------------------------\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        logger.info("\t........请求响应时间为：" + (end - start) + "（毫秒）");
    }

    /**
     * 自定义实现线程异步
     */
    @RequestMapping(value = { "/async" }, method = RequestMethod.GET)
    public void mineAsync() {
        long startTime = System.currentTimeMillis();
        System.out.println("开始时间"+startTime);
        for (int i = 0; i < 10; i++) {
            try {
                asyncService.doMineAsync(i);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("结束时间"+endTime);
        System.out.println("所用时间="+(endTime-startTime));
    }
    /**
     * 同步测试
     */
    @RequestMapping(value = { "/sync" }, method = RequestMethod.GET)
    public void mineAsync2() {
        long startTime = System.currentTimeMillis();
        System.out.println("开始时间="+startTime);
        for (int i = 0; i < 10; i++) {
            try {
                asyncService.doMineAsync2(i);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("结束时间="+endTime);
        System.out.println("所用时间="+(endTime-startTime));
    }
    /**
     * 自定义实现线程异步2
     */
    @RequestMapping(value = { "/async2" }, method = RequestMethod.GET)
    public void mineAsync3() {
        try {
            long startTime = System.currentTimeMillis();
            System.out.println("开始时间"+startTime);
            Future<Long> longFuture = asyncService.subByAsync();
            Future<Long> longFuture1 = asyncService.subByAsync2();
            while (true) {
                if (longFuture.isDone()) {
                    long aLong = longFuture.get();
                    logger.info("异步任务1执行的时间是：" + aLong + "（毫秒）");
                    break;
                }
            }
            while (true) {
                if (longFuture1.isDone()) {
                    long aLong1 = longFuture1.get();
                    logger.info("异步任务2执行的时间是：" + aLong1 + "（毫秒）");
                    break;
                }
            }
            long endTime = System.currentTimeMillis();
            System.out.println("结束时间"+endTime);
            System.out.println("所用时间="+(endTime-startTime));
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    /**
     * 同步测试2
     */
    @RequestMapping(value = { "/sync2" }, method = RequestMethod.GET)
    public void mineAsync4() {
        try {
            long startTime = System.currentTimeMillis();
            System.out.println("开始时间"+startTime);
            long aLong = asyncService.subBySync();
            long aLong1 = asyncService.subBySync2();
            System.out.println("aLong="+aLong);
            System.out.println("aLong1="+aLong1);
            long endTime = System.currentTimeMillis();
            System.out.println("结束时间"+endTime);
            System.out.println("所用时间="+(endTime-startTime));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}